package com.dfdchem.imd.kaiwu.activity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.dfdchem.imd.kaiwu.R;
import com.dfdchem.imd.kaiwu.activity.base.BaseActivity;
import com.dfdchem.imd.kaiwu.constant.RouterPath;
import com.dfdchem.imd.kaiwu.interfaces.OnHttpResponseListener;
import com.dfdchem.imd.kaiwu.manager.DataManager;
import com.dfdchem.imd.kaiwu.model.AppPage;
import com.dfdchem.imd.kaiwu.model.Resp;
import com.dfdchem.imd.kaiwu.util.JSON;
import com.dfdchem.imd.kaiwu.util.RequestUtil;

import zuo.biao.library.util.Log;

@Route(path = RouterPath.ABOUT_ACTIVITY)
public class AboutActivity extends BaseActivity implements View.OnClickListener, OnHttpResponseListener {
    private static final String TAG = "AboutActivity";

    private RelativeLayout profile_item_privacy;
    private RelativeLayout profile_item_service;
    private RelativeLayout profile_item_secret;
    private RelativeLayout profile_item_clear_cache;
    private RelativeLayout profile_item_user_protocol;

    private static final int request_code_user_protocol = 1;
    private static final int request_code_privacy = 3;
    private static final int request_code_service = 4;
    private static final int request_code_secret = 5;

    /**
     * 启动这个Activity的Intent
     * @param context
     * @return Intent
     */
    public static Intent createIntent(Context context) {
        return new Intent(context, AboutActivity.class);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about);

        initView();
        initData();
        initEvent();
    }

    @Override
    public void initView() {
        Log.d(TAG, this.getClass().getSimpleName() + " initView");

        profile_item_privacy = (RelativeLayout) findViewById(R.id.profile_item_privacy);
        profile_item_service = (RelativeLayout) findViewById(R.id.profile_item_service);
        profile_item_secret = (RelativeLayout) findViewById(R.id.profile_item_secret);
        profile_item_clear_cache = (RelativeLayout) findViewById(R.id.profile_item_clear_cache);
        profile_item_user_protocol = (RelativeLayout) findViewById(R.id.profile_item_user_protocol);
    }

    @Override
    public void initData() {
        Log.d(TAG, this.getClass().getSimpleName() + " initData");
    }

    @Override
    public void initEvent() {
        Log.d(TAG, this.getClass().getSimpleName() + " initEvent");

        profile_item_privacy.setOnClickListener(this);
        profile_item_service.setOnClickListener(this);
        profile_item_secret.setOnClickListener(this);
        profile_item_clear_cache.setOnClickListener(this);
        profile_item_user_protocol.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.profile_item_privacy:
                showProgressDialog("加载中...");
                // 更新本地存储的用户信息
                runThread(TAG + "getPrivacy", new Runnable() {
                    @Override
                    public void run() {
                        RequestUtil.getDocumentByKey("privacy", request_code_privacy, AboutActivity.this);
                    }
                });
                break;
            case R.id.profile_item_service:
                showProgressDialog("加载中...");
                // 更新本地存储的用户信息
                runThread(TAG + "getService", new Runnable() {
                    @Override
                    public void run() {
                        RequestUtil.getDocumentByKey("service", request_code_service, AboutActivity.this);
                    }
                });
                break;
            case R.id.profile_item_secret:
                showProgressDialog("加载中...");
                // 更新本地存储的用户信息
                runThread(TAG + "getSecret", new Runnable() {
                    @Override
                    public void run() {
                        RequestUtil.getDocumentByKey("secret", request_code_service, AboutActivity.this);
                    }
                });
                break;
            case R.id.profile_item_user_protocol:
                showProgressDialog("加载中...");
                // 更新本地存储的用户信息
                runThread(TAG + "getUserProtocol", new Runnable() {
                    @Override
                    public void run() {
                        RequestUtil.getDocumentByKey("user_protocol", request_code_service, AboutActivity.this);
                    }
                });
                break;
            case R.id.profile_item_clear_cache:
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("提示：");
                builder.setMessage("是否清除缓存");
                builder.setIcon(R.drawable.icon_info);
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        showShortToast("清除缓存成功");
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });

                builder.create().show();
                break;
        }
    }


    @Override
    public void onHttpResponse(int requestCode, String resultJson, Exception e) {
        dismissProgressDialog();
        if (e != null) {
            Toast.makeText(this, "请求失败，" + e.getMessage(), Toast.LENGTH_LONG).show();
            return;
        }
        switch (requestCode) {
            case request_code_user_protocol:
            case request_code_secret:
            case request_code_privacy:
            case request_code_service:
                Resp<AppPage> appPage = JSON.parseResp(resultJson, AppPage.class);
                if (appPage != null && appPage.getData() != null) {
                    ARouter.getInstance()
                            .build(DataManager.getInstance().isX5() ? RouterPath.WEB_VIEW_X5_ACTIVITY : RouterPath.WEB_VIEW_ACTIVITY)
                            .withString("name", appPage.getData().getName())
                            .withString("content", appPage.getData().getContent())
                            .navigation(this);

                    // toActivity(RouterPath.WEB_VIEW_ACTIVITY, R.anim.right_push_in, R.anim.left_push_out, false);
                } else {
                    Toast.makeText(this, "数据为空，请重试", Toast.LENGTH_LONG).show();
                }

                break;
        }
    }
}